import * as types from '../types'
import BaseUtil from '../../utils/baseUtil'

const state = {
  user: null,
  loginStatus:false,
  currentMenu:1
}

const mutations = {
  [types.FIRSTLOGIN]: (state, userInfo) => {
      if(userInfo)  BaseUtil.setStore('user',userInfo)
      else BaseUtil.removeStore('user')
      state.user = userInfo;
      state.loginStatus = true;
  },
  [types.LOGOUT]: (state) => {
      BaseUtil.removeStore('user')
      BaseUtil.removeStore('currentMenu')
      state.user = null;
      state.currentMenu = null;
      state.loginStatus = false;
  },
  [types.CurrentMenu]: (state,currentMenu) => {
      if(currentMenu || currentMenu == 0) BaseUtil.setStore('currentMenu',currentMenu)
      else BaseUtil.removeStore('currentMenu')
      state.currentMenu = currentMenu;
  },
}

const actions = {
  LoginAction ({ commit },userInfo) {
    commit(types.FIRSTLOGIN, userInfo);
  },
  LogOutAction ({ commit }) {
    commit(types.LOGOUT);
  },
  PushCurrentMenu ({ commit },currentMenu) {
    commit(types.CurrentMenu, currentMenu);
  }
}

export default {
  state,
  mutations,
  actions
}
